import axios from "axios";
import Auth from "./Auth";
import i18next from "../i18n";
import TokenUtil from "../utils/TokenUtil";
import { showMessage } from "../component/Manager/Message";

export const dataAcquisitionPlatformURL = "/xscloud";

export const getDataAcquisitionPlatformURL = () => {
    return dataAcquisitionPlatformURL;
};

export const service = axios.create({
    baseURL: getDataAcquisitionPlatformURL(),
    validateStatus: function (status) {
        return status === 500 || status === 200 ? true : false;
    },
});
 
service.interceptors.request.use(
    (config) => {
        if (config.headers.isNoNeedToken) return config;       
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    (response) => {
        const res = response.data;
        const config = response.config;
        if (res.code && res.code !== 200) {
            if (res.code === 401) {
                showMessage({
                    message: res.msg,
                    type: "error",
                    duration: 5 * 1000,
                });
                return;
                // return store.dispatch("user/resetToken");
            }
            // if (
            //     config &&
            //     (config.url.includes("checkJsonInfo") ||
            //         !config.url.includes("/resource/file/getFileUrl"))
            // ) {
            //     showMessage({
            //         message: res.msg || "Error",
            //         type: "error",
            //         duration: 5 * 1000,
            //     });
            //     return;
            // }
            return res;
        } else {
            // if (config.url.includes("/resource/file/local/tags.xlsx")) {
            //     return res;
            // }
            return res;
        }
    },
    (error) => {
        return Promise.reject(error);
    }
);

export default service;
